package org.misalen.web.server.controllers.user;

import java.math.BigInteger;
import java.util.Date;

import org.misalen.annotations.ControllerApi;
import org.misalen.annotations.InterfaceApi;
import org.misalen.web.database.model.mysql.pro.ProUser;
import org.misalen.web.database.model.mysql.pro.ProUserExtractCash;
import org.misalen.web.database.util.PaginationFrom;
import org.misalen.web.server.config.ErrorCode;
import org.misalen.web.server.config.ServerResult;
import org.misalen.web.server.config.ServerUrl;
import org.misalen.web.server.config.Uncertain;
import org.misalen.web.server.controllers.BaseController;
import org.misalen.web.server.exception.CustomerException;
import org.misalen.web.server.forms.user.WithdrawalApplyForm;
import org.misalen.web.server.forms.user.WithdrawalRecordForm;
import org.misalen.web.server.result.Empty;
import org.misalen.web.server.util.FormUtil;
import org.misalen.web.service.sms.info.AliInfoType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ControllerApi(value = "用户提现", description = "申请、记录", index = 400)
@RequestMapping(value = ServerUrl.UserWithdrawalControllerUrl)
public class UserWithdrawalController extends BaseController {

	@RequestMapping(value = "/apply", method = { RequestMethod.POST })
	@InterfaceApi(value = "提现申请", resultClass = { Empty.class })
	public @ResponseBody ServerResult apply(@RequestBody WithdrawalApplyForm form) throws CustomerException {
		FormUtil.validate(form);
		ProUser user = proUserRepository.get(ProUser.class, form.getUserId());
		if (user == null) {
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_10005);
		} else if (user.getBalance() < form.getAmount()) {
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_10010);
		} else {
			long sendByTimeCount = proSmsCodeRepository.countMobileTimeAndCode(user.getMobile(), form.getAuthCode(),
					Uncertain.smsValidity, AliInfoType.userWithDrawCode.type);
			if (sendByTimeCount == 0) {
				return ServerResult.error(ErrorCode.SYS_ERROR_CODE_00003);
			} else {
				proUserRepository.updateBalance(-form.getAmount().intValue(), user.getPrimaryKey());
				ProUserExtractCash userExtractCash = new ProUserExtractCash();
				userExtractCash.setAddTime(new Date());
				userExtractCash.setAmount(BigInteger.valueOf(form.getAmount()));
				userExtractCash.setAccount(form.getAccount());
				userExtractCash.setType(form.getProExtractType());
				userExtractCash.setUserId(user.getPrimaryKey());
				userExtractCash.setState("not-operating");
				proUserExtractCashRepository.save(userExtractCash);
			}
		}
		return ServerResult.ok();
	}

	@RequestMapping(value = "/record", method = { RequestMethod.POST })
	@InterfaceApi(value = "提现记录", resultClass = { PaginationFrom.class, ProUserExtractCash.class }, include = {
			"amount", "account", "state", "type", "addTime", "processingTime" })
	public PaginationFrom<ProUserExtractCash> record(@RequestBody WithdrawalRecordForm form) throws CustomerException {
		FormUtil.validate(form);
		PaginationFrom<ProUserExtractCash> pagination = formToPage(form, ProUserExtractCash.class);
		ProUserExtractCash allclass = new ProUserExtractCash();
		allclass.setUserId(form.getUserId());
		if (form.getProExtractState() != null) {
			allclass.setState(form.getProExtractState());
		}
		return proUserExtractCashRepository.findPageByBean(allclass, pagination);
	}

}
